//
//  StarAccountViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "StarAccountViewController.h"
#import "StarAccountTopView.h"

@interface StarAccountViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableDictionary *_accountDict;
}
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation StarAccountViewController
static NSString *accountCellID = @"accountCellID";

- (void)StarAccountRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kStarAccount];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            _accountDict = responseObject[@"data"];
            [self creatTableView];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self StarAccountRequest];
    [self setNav];
    [self setUpUI];
}

- (void)setNav {
    self.navigationItem.title = @"我的账户";
}
#pragma mark -- UI
- (void)setUpUI {
    _accountDict = [NSMutableDictionary dictionaryWithCapacity:1];
    //tableView
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    NSString *balance = [NSString stringWithFormat:@"%@元",_accountDict[@"u_balance"]];
    self.tableView.tableHeaderView = [self creatTopViewWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 200) Amount:balance];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:accountCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:accountCellID];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

#pragma mark -- topView sectionView

- (UIView *)creatTopViewWithFrame:(CGRect)frame Amount:(NSString *)amount {
    StarAccountTopView *topView = [[NSBundle mainBundle] loadNibNamed:@"StarAccountTopView" owner:self options:nil].lastObject;
    topView.starBalance.text = amount;
    topView.starIncomeLabel.text = KString(_accountDict[@"u_use"]);
    topView.RedPaperCost.text = KString(_accountDict[@"u_obtain"]);
    return topView;
}

- (UIView *)creatSecitonViewWtihFrame:(CGRect)frame Income:(NSString *)income Redpaper:(NSString *)redpaper {
    UIView *sectionView = [[UIView alloc] initWithFrame:frame];
    
    UILabel *incomeLable = [[UILabel alloc] init];
    incomeLable.font = UIFont(11);
    incomeLable.text = income;
    UILabel *redpaperLable = [[UILabel alloc] init];
    redpaperLable.font = UIFont(11);
    redpaperLable.text = redpaper;
    [sectionView addSubview:incomeLable];
    [sectionView addSubview:redpaperLable];
    
    [incomeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sectionView);
        make.left.equalTo(sectionView.mas_left).offset(20);
    }];
    [redpaperLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sectionView);
        make.right.mas_equalTo(sectionView.mas_right).offset(-20);
    }];
    
    return sectionView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
